//
//  USLoveCompassManager.m
//  us
//
//  Created by karry on 2025/4/7.
//

#import "USLoveCompassManager.h"

@implementation USLoveCompassManager

- (instancetype)init {
    self = [super init];
    if (self) {
        self.locationManager = [[CLLocationManager alloc] init];
        self.locationManager.delegate = self;
        [self.locationManager requestWhenInUseAuthorization];
        self.locationManager.headingFilter = kCLHeadingFilterNone;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    }
    return self;
}

- (void)startUpdating {
    [self.locationManager startUpdatingHeading];
    [self.locationManager startUpdatingLocation];
}

#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
    if (self.headingUpdateBlock) {
        self.headingUpdateBlock(newHeading);
    }
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    CLLocation *current = locations.lastObject;
    if (self.locationUpdateBlock) {
        self.locationUpdateBlock(current);
    }
}

@end
